package com.centerm.demo;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.centerm.bluetooth.ibridge.BluetoothIBridgeDevice;
import com.centerm.bluetooth.lkl.demo.R;
import com.centerm.util.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by linwenhui on 2017/5/2.
 */

public class BluetoothDialog extends Dialog implements DialogInterface.OnKeyListener {
    private ListView listView;
    private BluetoothAdapter adapter;
    public Button btnReSearch;
    private EditText edtxt;
    private List<BluetoothIBridgeDevice> deviceList;
    private String filterText;
    private IItemSelListener itemSelListener;

    public BluetoothDialog(Context context) {
        super(context, R.style.DialogStyle);
        setContentView(R.layout.dialog_bluetooth);
        setCancelable(false);
        listView = (ListView) findViewById(R.id.lstvw);
        btnReSearch = (Button) findViewById(R.id.btnReSearch);
        edtxt = (EditText) findViewById(R.id.edtxt);
        edtxt.addTextChangedListener(new TextWatcherImp());
        adapter = new BluetoothAdapter(context);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(adapter);
        setOnKeyListener(this);
        deviceList = new ArrayList<>();
    }

    public void add(BluetoothIBridgeDevice device) {
        if (!deviceList.contains(device))
            deviceList.add(device);
        if (TextUtils.isEmpty(filterText) || device.getDeviceName().lastIndexOf(filterText) != -1)
            adapter.add(device);
    }

    public void setItemSelListener(IItemSelListener itemSelListener) {
        this.itemSelListener = itemSelListener;
    }

    public void clear() {
        deviceList.clear();
        adapter.clear();
    }

    @Override
    public void show() {
        edtxt.setText("");
        super.show();
    }

    public BluetoothIBridgeDevice getSel() {
        return adapter.getSel();
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return false;
    }

    private class TextWatcherImp implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            filterText = s.toString();
            Logger.i(" == afterTextChanged == " + s.toString() + " == " + JSON.toJSONString(deviceList));
            List<BluetoothIBridgeDevice> tmpList = new ArrayList<>();
            int index;
            for (BluetoothIBridgeDevice dev : deviceList) {
                if (dev == null || TextUtils.isEmpty(dev.getDeviceName()))
                    continue;
                index = dev.getDeviceName().lastIndexOf(filterText);
                Logger.i(" == index == " + index + " == " + dev.getDeviceName());
                if (index != -1 && !tmpList.contains(dev)) {
                    tmpList.add(dev);
                }
            }
            adapter.clear();
            adapter.addAll(tmpList);
        }
    }

    private class BluetoothAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

        private List<BluetoothIBridgeDevice> devices;
        private LayoutInflater inflater;
        private int sel = -1;

        public BluetoothAdapter(Context context) {
            this.devices = new ArrayList<>();
            inflater = LayoutInflater.from(context);
        }

        public void addAll(List<BluetoothIBridgeDevice> devices) {
            this.devices.addAll(devices);
            notifyDataSetChanged();
        }

        public void add(BluetoothIBridgeDevice device) {
            if (!this.devices.contains(device)) {
                this.devices.add(device);
                notifyDataSetChanged();
            }
        }

        public void clear() {
            devices.clear();
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return devices.size();
        }

        public BluetoothIBridgeDevice getSel() {
            if (sel != -1)
                return getItem(sel);
            return null;
        }

        @Override
        public BluetoothIBridgeDevice getItem(int position) {
            return devices.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            }
            TextView txtvw = (TextView) convertView;
            txtvw.setTextColor(Color.BLACK);
            BluetoothIBridgeDevice device = getItem(position);
            txtvw.setText("蓝牙名：" + device.getDeviceName() + "\n地址：" + device.getDeviceAddress());
            return convertView;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            sel = position;
            if (itemSelListener != null)
                itemSelListener.onItemClick(getItem(sel));
        }
    }

    public interface IItemSelListener {
        void onItemClick(BluetoothIBridgeDevice device);
    }

}
